package com.google.code.sysarch.common.util;

public class Parameter {

	private String[] args;
	public boolean strict = false;
	public boolean warnings = false;
	public String prefix = "-";
	public String usage = "";
	
	
	public Parameter(String[] args) {
		this.args = args;
	}

	public double getDouble(int argNum, double def) {
		return getDouble(argNum, def, Double.MIN_VALUE, Double.MAX_VALUE);
	}
	public int getInt(int argNum, int def) {
		return getInt(argNum, def, Integer.MIN_VALUE, Integer.MAX_VALUE);
	}

	public double getDouble(int argNum, double def, double min, double max) {
		double value = def;	
		if (args.length < argNum) {
			warn("Argument "+argNum+" does not exist. Using default value '"+def+"'.");	
			return def;
		}
		
		try {
			value = Double.parseDouble(args[argNum-1]);
		} catch (NumberFormatException e) { 
			warn("NumberFormatException for parameter "+argNum+" (Double). Using default value '"+def+"'.");
		};
		
		if (value < min) {
			warn("Parameter "+argNum+" is too small. Using minimum '"+min+"'.");
			if (strict) { return def; }
			value = min;
		}
		if (value > max) {
			warn("Parameter "+argNum+" is too big. Using maximum '"+max+"'.");
			if (strict) { return def; }		
			value = max;
		}
		
		return value;
	}

	public int getInt(int argNum, int def, int min, int max) {
		int value = def;	
		if (args.length < argNum) {
			warn("Argument "+argNum+" does not exist. Using default value '"+def+"'.");		
			return def;
		}
		
		try {
			value = Integer.parseInt(args[argNum-1]);
		} catch (NumberFormatException e) { 
			warn("NumberFormatException for parameter "+argNum+" (Integer). Using default value '"+def+"'.");
		};
		
		if (value < min) {
			warn("Parameter "+argNum+" is too small. Using minimum '"+min+"'.");
			if (strict) { return def; }
			value = min;
		}
		if (value > max) {
			warn("Parameter "+argNum+" is too big. Using maximum '"+max+"'.");
			if (strict) { return def; }		
			value = max;
		}
		
		return value;
	}
	
	public String getString(int argNum, String def) {
		if (args.length < argNum) {
			warn("Argument "+argNum+" does not exist. Using default value '"+def+"'.");		
			return def;
		}
		return args[argNum-1];
	}
	
	public boolean getFlag(String name) {
		for (int i = 0; i < args.length; i++) {
			if (args[i].equals(this.prefix+name)) {
				return true;
			}
		}
		return false;
	}	
	
	public String getString(String name, String def) {
		int match = -1;
		
		for (int i = 0; i < args.length - 1; i++) {
			if (args[i].equals(this.prefix+name) && match == -1) {
				match = i;
			}
		}
		
		if (match == -1) { 
			warn("Argument '"+name+"' does not exist. Using default value '"+def+"'.");	 		
			return def;
		}
		return args[match+1];
	}
	
	public int getInt(String name, int def, int min, int max) {
		int value = def;
		int match = -1;
		
		for (int i = 0; i < args.length - 1; i++) {
			if (args[i].equals(this.prefix+name) && match == -1) {
				match = i;
			}
		}
		
		if (match == -1) {
			warn("Argument '"+name+"' does not exist. Using default value '"+def+"'.");	 
			return def;
		}
		
		try {
			match++;
			value = Integer.parseInt(args[match]);
		} catch (NumberFormatException e) { 
			warn("NumberFormatException for parameter '"+name+"' (Integer). Using default value '"+def+"'.");
		};
		
		if (value < min) {
			warn("Parameter '"+name+"' is too small. Using minimum '"+min+"'.");
			if (strict) { return def; }
			value = min;
		}
		if (value > max) {
			warn("Parameter '"+name+"' is too big. Using maximum '"+max+"'.");
			if (strict) { return def; }		
			value = max;
		}
		
		return value;
	}
	
	public double getDouble(String name, double def, double min, double max) {
		double value = def;
		int match = -1;
		
		for (int i = 0; i < args.length - 1; i++) {
			if (args[i].equals(this.prefix+name) && match == -1) {
				match = i;
			}
		}
		
		if (match == -1) {
			warn("Argument '"+name+"' does not exist. Using default value '"+def+"'.");	  
			return def;
		}
		
		try {
			match++;
			value = Double.parseDouble(args[match]);
		} catch (NumberFormatException e) { 
			warn("NumberFormatException for parameter '"+name+"' (Double). Using default value '"+def+"'.");
		};
		
		if (value < min) {
			warn("Parameter '"+name+"' is too small. Using minimum '"+min+"'.");
			if (strict) { return def; }
			value = min;
		}
		if (value > max) {
			warn("Parameter '"+name+"' is too big. Using maximum '"+max+"'.");
			if (strict) { return def; }		
			value = max;
		}
		
		return value;
	}	
	
	public void warn(String message) {
		if (warnings == true) {
			System.out.println("Warning: "+message);
		}
	}
}